package cn.edu.lcvc.ebuy_lyj.service;
import cn.edu.lcvc.ebuy_lyj.dao.AdminDao;
import cn.edu.lcvc.ebuy_lyj.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service  //相当于 <bean id="adminBean" class="cn.edu.lcvc.ebuy_ruanjian2.bean.AdminBean"/>(spring在项目初始化的时候创建一个对象)
public class AdminBean {
    public AdminBean(){
        System.out.println("adminBean创建了一个对象");
    }
    @Autowired
    private AdminDao adminDao;

    public boolean login(String username, String password) {
        boolean result=false;//默认登录失败
        if (adminDao.countByUsernameAndPassword(username, password)==1){
            result=true;
        }
        return result;

    }

    public Admin getAdmin(String username) {
        return adminDao.getUsername(username);

    }
    public List<Admin> findAll(){
        return adminDao.findAll();
    }

    public boolean deleteAdmin(Integer id){
        boolean result=false;
        if (id!=null){
            adminDao.deleteById(id);
            if (adminDao.deleteById(id)>0){
                result=true;
            }
        }
        return result;
    }
    public boolean adminAdd(String username, String password, String name, int sex, boolean unlocked){
        boolean result=false;
        Date createTime=new Date();
        int a=adminDao.addAdmin(username,password,name,sex,unlocked,createTime);
        //如果返回的是1 则说明添加成功
        if (a>0){
            result=true;
        }
        return result;
    }
    public boolean isEntryUsername(String username){
        boolean res=false;//默认不存在
        if (adminDao.getUsername(username)!=null){ //如果查询到的结果为空 说明用户已经存在
            res=true;
        }
        return res;

    }
    public boolean updateAdmin(Integer id,String username,String name,int sex,boolean unlocked){
        boolean res=false;
        Date createTime=new Date();
        if (adminDao.updateAdmin(id,username,name,sex,unlocked,createTime)>0){
            res=true;
        }
        return res;
    }
    public Admin getAdminById(Integer id){
        return adminDao.getAdminById(id);
    }
}
